#include "agencymgr.h"
#include "json.h"



AgencyMgr::AgencyMgr()
{
}
AgencyMgr::~AgencyMgr()
{
}

bool AgencyMgr::Update(stAgency&& agency)
{
    if (agency.name.empty()) 
    {
        return false;
    }
    m_mgr[agency.name] = std::move(agency);
    return true;
}
bool AgencyMgr::Update(const stAgency& agency)
{
    if (agency.name.empty()) 
    {
        return false;
    }
    m_mgr[agency.name] = agency;
    return true;
}
stAgency* AgencyMgr::Get(const std::string& name)
{
    auto it = m_mgr.find(name);
    if (it == m_mgr.end())
    {
        return nullptr;
    }
    return &it->second;
}

bool AgencyMgr::Init(const std::string& conf)
{
    if (conf.empty())
    {
        return false;
    }
    else
    {
        Json::Value json;
        Json::Reader reader;
        if (!reader.parse(conf, json))
        {
            return false;
        }
        if (!json.isMember("agency") || !json["agency"].isArray()|| json["agency"].empty())
        {
            return false;
        }

        for (auto i = 0; i < json["agency"].size(); i++)
        {
            auto& item = json["agency"][i];
                
            stAgency agency;
            if (item.isMember("name") && item["name"].isString())
            {
                agency.name = item["name"].asString();
            }
            if (item.isMember("key") && item["key"].isString())
            {
                agency.key = item["key"].asString();
            }
            if (item.isMember("api") && item["api"].isString())
            {
                agency.api = item["api"].asString();
            }
            if (item.isMember("currency") && item["currency"].isString())
            {
                agency.currency = item["currency"].asString();
            }
            m_mgr[agency.name] = agency;
        }
    }
    return true;
}








